package yambal.net.google.Photo
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	import yambal.net.google.events.PicasaPhotoEvent;
	
	[Event(name="update_photo", type='yambal.net.google.events.PicasaPhotoEvent')]
	public class PicasaPhoto extends EventDispatcher
	{
		public var album:PicasaAlbum;
		
		public var id:String;
		public var title:String;
		public var summary:String;
		public var version:String;
		public var access:String;
		public var width:uint;
		public var height:uint;
		public var size:uint;
		public var timestamp:String;
		
		public var src:String;
		public var s_src:String;
		public var m_src:String;
		public var l_src:String;
		
		public var edit_mata_endpoint:String;

		public override function toString():String{
			return "PicasaPhoto{id:" + this.id + " " + 
					"title:" + this.title + " " + 
					"summary:" + this.summary + " " + 
					"version:" + this.version + " " + 
					"access:" + this.access + " " + 
					"width:"+ this.width + " " + 
					"height:" + this.height + " " + 
					"size:" + this.size + " " + 
					"timestamp:"+ this.timestamp + " }"
		}
		
		// -----------------------------------------------------------------
		/**
		 * Metadataを更新する
		 **/
		public function updateMetaData(file_name:String, summary:String):void{
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, this.onMetaEdit);
			
			var req:URLRequest = new URLRequest();
			req.requestHeaders.push(new URLRequestHeader('Authorization', 'GoogleLogin auth=' + this.album.picasa.Auth.auth));
			req.requestHeaders.push(new URLRequestHeader('Content-Type', 'application/atom+xml'));
			req.method = URLRequestMethod.PUT;
			req.url = this.edit_mata_endpoint;
			
			req.data = new XML("<entry xmlns='http://www.w3.org/2005/Atom'>" + 
					"<title>" + file_name + "</title>" + 
					"<summary>" + summary + "</summary>" + 
					"<category scheme='http://schemas.google.com/g/2005#kind'" + 
					"term='http://schemas.google.com/photos/2007#photo'/>" + 
					"</entry>");
			
			loader.load(req);
		}
		
		private function onMetaEdit(e:Event):void{
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, this.onMetaEdit);
			
			var res:XML = new XML(loader.data)
			var modified:PicasaPhoto = this.album.parseToPhoto(res);
			
			this.id = modified.id;
			this.title = modified.title;
			this.summary = modified.summary;
			this.version = modified.version;
			this.access = modified.access;
			this.width = modified.width;
			this.height = modified.height;
			this.size = modified.size;
			this.timestamp = modified.timestamp;
			this.edit_mata_endpoint = modified.edit_mata_endpoint;
			
			this.dispatchEvent(new PicasaPhotoEvent(PicasaPhotoEvent.UPDATE_PHOTO, this));
			this.album.onUpdatePhoto(this);
		}
	}
}